#include "tch.h"
Visitor *p;//申请堆内存，存放登录信息
Visitor *v;//活跃指针
int exit_flag;//系统即将退出的标志
bool quit_flag;
void Insertgrade(const int id,const char level)
{
	if(!Findid(id))
	{
		printf("此%s不存在!\n",Translate(Lowerlevel(level)));
		Anykey();
		return;
	}
	Visitor *dest=Findmember(id);
	if(!Levelcmp(level,dest->level))
	{
		printf("此学工号对应的身份是%s,您无权对其进行操作!\n",Translate(dest->level));
		Anykey();
		return;
	}
	printf("请依次键入学生的语文数学和英语成绩:\n");
	scanf("%f%f%f",&dest->grade[0],&dest->grade[1],&dest->grade[2]);
	Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
	printf("成绩成功录入!\n");
	Anykey();
	return;	
}
void Batchimport(const char* filename,const char level)
{
	CLC;
	FILE* frp=fopen(filename,"r");
	if(NULL == frp)
	{
		perror("fopen");	
		Anykey();
		return;
	}
	Visitor *dest = &p[0];
	while(Translate(dest->level)!=NULL) dest++;
	int ret=0;
	char name[255]={};
	int error=0;
	int skip=0;
	while(1)
	{
		if(Getgroupnumber('o')>MAX_NUMBER)
		{
			printf("系统成员已满,导入失败!\n");
			break;
		}
		if(fgetc(frp)==EOF)
				break;
		fscanf(frp,"%s %c",name,&dest->sex);
		ret++;
		if(strlen(name)>20)
		{
			printf("导入失败!第%d个成员名字过长\n",ret);
			dest--;
			error++;
		}
		else if(Findname(name))
		{
			dest--;
			skip++;
		}
		else if(dest->sex!='m'&&dest->sex!='w')
		{
			printf("导入失败!第%d个成员性别不正常!\n",ret);
			dest--;
			error++;
		}
		else
		{
			strcpy(dest->name,name);
			Full(dest,Lowerlevel(level));
			Writesigninmsg("signin.txt",dest);
		}
		fgetc(frp);
		dest++;
	}
	printf("导入中...\n");
	WAIT(3);
	dest-=(ret-error-skip);
	while(Translate(dest->level)!=NULL)
	{
		printf("%s %s %d\n",dest->name,Translate(dest->sex),dest->id);
		dest++;
	}
	printf("导入完成!其中成功导入%d次,跳过%d次,失败%d次\n",ret-error-skip,skip,error);
	Anykey();
	return;
}
void Gradebatch(const char* filename,const char level)
{
	CLC;
	FILE* frp=fopen(filename,"r");
	if(NULL == frp)
	{
		perror("fopen");	
		Anykey();
		return;
	}
	Visitor *dest = &p[0];
	char name[20] = {};
	int ret=0;
	int error=0;
	while(1)
	{
		if(fgetc(frp)==EOF)
				break;
		fscanf(frp,"%s",name);
		dest = Findname(name);
		if(dest!=NULL)
		{
			fscanf(frp,"%f %f %f",&dest->grade[0],&dest->grade[1],&dest->grade[2]);
			Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));	
			ret++;
		}
		else
		{
			error++;
			printf("导入失败，成员中没有'%s'\n",name);	
		}
		fgetc(frp);
	}
	printf("导入中...\n");
	WAIT(3);
	printf("成功导入%d次,导入完成...\n",ret);
	Anykey();
	return;
}
void Delete(const int id,const char level)
{
	if(!Findid(id))
	{
		printf("此%s不存在!\n",Translate(Lowerlevel(level)));
		Anykey();
		return;
	}
	Visitor *dest=Findmember(id);
	if(dest->level==level&&level=='a'){ 
			printf("您不能删除自己的账号!\n");
			Anykey();
			return;
	}
	if(Levelcmp(level,dest->level)==false)
	{
		printf("此学工号对应的身份是%s,您无权对其进行操作!\n",Translate(dest->level));
		Anykey();
		return;
	}
	int sid=0;
	while(sid!=id)
	{
		CLC;
		if(sid!=0) printf("学工号输入有误，请检查!");
		printf("确认删除的话请再输入一次其的学工号:\n");
		scanf("%d",&sid);
	}
	printf("删除%s成功!\n",Translate(Lowerlevel(level)));
	if(dest->level=='t')
	{
		dest->level='q';
		Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
	}
	else if(dest->level=='s')
	{
		dest->level='d';
		Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
	}
	else
	{
		dest->level='o';
	}
	Anykey();
	return;
}
void Modifygrade(const int id,const char level)
{
	while(1)
	{
		CLC;
		Visitor *dest=Findmember(id);
		Displayonemsg(dest->id,level);
		printf("请问您要修改以上哪些数据呢?\n1---->语文成绩\n2---->数学成绩\n3---->英语成绩\n回车---->返回主界面\n");
		int choice = getch();	
		switch(choice)
		{
			case 49:printf("修改的语文成绩为:");scanf("%f",&dest->grade[0]);Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));break;
			case 50:printf("修改的数学成绩为:");scanf("%f",&dest->grade[1]);Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));break;
			case 51:printf("修改的英语成绩为:");scanf("%f",&dest->grade[2]);Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));break;
			case 10:return; break;
			default:printf("输入错误!\n");
		}
	}
}
void Modifyinfo(const int id,const char level)
{
	while(1)
	{
		CLC;
		Visitor *dest=Findmember(id);
		Displayonemsg(dest->id,level);
		printf("请问您要修改以上哪些数据呢?\n1---->姓名\n2---->性别\n回车键---->返回主界面\n");
		int choice = getch();
		switch(choice)
		{
			case 49:printf("修改的姓名为:");
			while(scanf("%s",dest->name),strlen(dest->name)>20)
			{
				CLC;
				printf("名字长度不要超过20字符(一个汉字算2字符)!\n");	
				printf("修改的姓名为:");
				Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
			}
			if(Samename(dest->name))
			{
				printf("由于系统中有重名用户,自动生成乱码后缀\n");
				strcat(dest->name,"1");
				printf("修改姓名为%s\n",dest->name);
				Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
			}
			break;
			case 50:printf("修改的性别为:");
			scanf("%c",&dest->sex);
			while(dest->sex!='w'&&dest->sex!='m')
			{
				CLC;
				printf("性别输入错误!请重试\n");	
				printf("修改的性别为:");
				scanf(" %c",&dest->sex);
				Modifysigninmsg("signin.txt",dest,getCurrentIndex(dest->id));
			}
			break;
			case 10:return; break;
			default:printf("输入错误!\n");
		}
	}
}
void Modify(int id,const char cmd,const char level)
{
	if(!Findid(id))
	{
		printf("此%s不存在!\n",Translate(Lowerlevel(level)));
		Anykey();
		return;
	}
	if(cmd!='g'&&cmd!='i')
	{
		printf("命令错误!\n");
		Anykey();
		return;
	}
	Visitor *dest=Findmember(id);
	if(!Levelcmp(level,dest->level))
	{
		printf("您无权执行此命令!\n");	
		Anykey();
		return;
	}
	switch(cmd)
	{
		case 'g':Modifygrade(id,level);break;	
		case 'i':Modifyinfo(id,level); break;
		default :break;
	}	
	Anykey();
	return;
}
void Displayallmsg(const char cmd,const char level)
{
	Visitor *dest=&p[0];
	if(cmd!='a'&&cmd!='q'&&cmd!='d'&&cmd!='s'&&cmd!='t')
	{
		printf("命令错误!\n");
		Anykey();
		return;
	}
	if(Levelcmp(level,cmd)==false)
	{
		printf("您无权执行此命令!\n");	
		Anykey();
		return;
	}
	CLC;
	while(Translate(dest->level)!=NULL)
	{
		if(dest->level==cmd)
	printf("姓名:%s 性别:%s 学工号:%d 权限:%s\n",dest->name,Translate(dest->sex),dest->id,Translate(dest->level));
		dest++;
	}
	printf("信息已经全部输出完毕!\n");
	Anykey();
	return;
}

